#include "xmltabmodel.h"

XMLTabModel::XMLTabModel(QObject * parent)
    : QAbstractTableModel(parent)
{
columns_count = 0;
rows_count = 0;
ignored_rows = 1;
}

XMLTabModel::~XMLTabModel()
{
}

void XMLTabModel::set_data( const QList< QMap<int, QString> > & data,
                            bool ncat_export )
{
model_data = data;
QListIterator< QMap<int, QString> > l_it(model_data);
QVector<int> max_check;
    while ( l_it.hasNext() )
    {
    QMap<int, QString> m = l_it.next();
    QList<int> keys_list = m.keys();
    int max = 0;
        for ( int z = 0; z < keys_list.size(); ++z )
        {
            if ( max < keys_list.at(z) )
            {
            max = keys_list.at(z);
            }
        }
    max_check.push_back(max);
    }

    for ( int z = 0; z < max_check.size(); ++z )
    {
        if ( columns_count < max_check.at(z) )
        {
        columns_count = max_check.at(z);
        }
    }
QMap<int, QString> first_row;
    if ( true != ncat_export )
    {
        for ( int z = 1; z <= columns_count; z++ )
        {
        first_row[z] = tr("Skip Column");
        }
    }
    else
    {
    set_ncat_map();
        for ( int z = 1; z <= columns_count; z++ )
        {
        first_row[z] = ncat_columns.value(z - 1);
        }
    }
model_data.prepend(first_row);
rows_count = model_data.size();
}

int XMLTabModel::rowCount( const QModelIndex & ) const
{
return rows_count;
}

int XMLTabModel::columnCount(const QModelIndex & /* parent */) const
{
return columns_count;
}

QVariant XMLTabModel::data( const QModelIndex & index,
                            int role ) const
{
    if ( role == Qt::DisplayRole || role == Qt::EditRole)
    {
    QString s = model_data.at( index.row() ).value( index.column() + 1 );
    QVariant qv = QVariant(s);
    return qv;
    }
    
    if (index.row() == 0)
    {
        if (role == Qt::ForegroundRole && index.row() == 0)
        {
        return qVariantFromValue( QColor( Qt::white ) );
        }

        if (role == Qt::BackgroundRole && index.row() == 0)
        {
        return qVariantFromValue( QColor( Qt::darkBlue ) );
        }
    }

    if ( index.row() > 0 && index.row() <= ignored_rows )
    {
        if ( role == Qt::BackgroundRole )
        {
        return qVariantFromValue( QColor( Qt::gray ) );
        }
    }

return QVariant();
}

Qt::ItemFlags XMLTabModel::flags(const QModelIndex & index) const
{
    if ( index.row() == 0 )
    {
    return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
    }
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

bool XMLTabModel::setData ( const QModelIndex & index,
                            const QVariant & value,
                            int role )
{
int role_dummy;
role_dummy = role;
    if ( index.row() == 0 )
    {
    QMap<int, QString> m;
    m = model_data[0];
    m[ index.column() + 1 ] = value.toString();
        if ( value.toString() != tr("Description") )
        {
            for ( int x = 1; x <= columns_count; x++ )
            {
                if ( x != ( index.column() + 1 ) )
                {
                    if ( m.value(x) == value.toString() )
                    {
                    m[x] = tr("Skip Column");
                    }
                }
            }
        }
    model_data[0] = m;
    QModelIndex first = this->index(0,0);
    QModelIndex last = this->index( 0, columns_count - 1 );
    emit QAbstractTableModel::dataChanged(first, last);
    return true;
    }

return false;
}

void XMLTabModel::set_ignored_rows(int rows)
{
ignored_rows = rows;
reset();
}

void XMLTabModel::set_ncat_map()
{
ncat_columns.clear();

Schema * schema = new Schema;
    if ( NULL == schema )
    {
    return;
    }
QMap<int, QString> headers = schema->headers();

ncat_columns[0]  = tr("Skip Column");
    for ( int i = 1; i < headers.size(); ++i )
    {
    ncat_columns[i] = headers.value(i);
    }
ncat_columns[headers.size() + 0] = tr("Description");
ncat_columns[headers.size() + 1] = tr("Description");
ncat_columns[headers.size() + 2] = tr("Description");
ncat_columns[headers.size() + 3] = tr("Description");
ncat_columns[headers.size() + 4] = tr("Description");
}
